import Vue from 'vue'
import Router from 'vue-router'
import Home from '@/pages/home/Home'
import Login from '@/pages/login/Login'
import Forget from '@/pages/forget/Forget'
import Food from '@/pages/food/Food'
import Shop from '@/pages/shop/Shop'
import CheckOut from '@/pages/checkout/CheckOut'
import CheckOutAddress from '@/pages/checkout/children/Address'
import CheckOutAdd from '@/pages/checkout/children/children/Add'
import CheckOutEdit from '@/pages/checkout/children/children/Edit'
import CheckOutSearchAddress from '@/pages/checkout/children/children/children/SearchAddress'
import Remark from '@/pages/checkout/children/Remark'
import Invoice from '@/pages/checkout/children/Invoice'
import UserValidation from '@/pages/checkout/children/UserValidation'
import Payment from '@/pages/checkout/children/Payment'
import Search from '@/pages/search/Search'
import Find from '@/pages/find/Find'
import Order from '@/pages/order/Order'
import OrderDetail from '@/pages/order/children/OrderDetail'
import Profile from '@/pages/profile/Profile'
import AccountInfo from '@/pages/profile/children/AccountInfo'
import UserName from '@/pages/profile/children/children/UserName'
import Address from '@/pages/profile/children/Address'
import Add from '@/pages/profile/children/children/Add'
import SearchAddress from '@/pages/profile/children/children/SearchAddress'

Vue.use(Router)

// 防止重复点击路由报错
const originalPush = Router.prototype.push
Router.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

export default new Router({
  routes: [
    {
      path: '/',
      redirect: '/home'
    }, {
      path: '/home',
      name: 'Home',
      component: Home,
      meta: { keepAlive: true }
    }, {
      path: '/login',
      name: 'Login',
      component: Login
    }, {
      path: '/forget',
      name: 'Forget',
      component: Forget
    }, {
      path: '/food',
      name: 'Food',
      component: Food
    }, {
      path: '/shop',
      name: 'Shop',
      component: Shop
    }, {
      path: '/checkout',
      name: 'CheckOut',
      component: CheckOut,
      children: [
        {
          path: 'address',
          name: 'CheckOutAddress',
          component: CheckOutAddress,
          children: [
            {
              path: 'add',
              name: 'CheckOutAdd',
              component: CheckOutAdd,
              children: [
                {
                  path: 'search',
                  name: 'CheckOutSearchAddress',
                  component: CheckOutSearchAddress
                }
              ]
            },
            {
              path: 'edit',
              name: 'CheckOutEdit',
              component: CheckOutEdit,
              children: [
                {
                  path: 'search',
                  name: 'CheckOutSearchAddress',
                  component: CheckOutSearchAddress
                }
              ]
            }
          ]
        },
        {
          path: 'remark',
          name: 'Remark',
          component: Remark
        },
        {
          path: 'invoice',
          name: 'Invoice',
          component: Invoice
        },
        {
          path: 'validation',
          name: 'UserValidation',
          component: UserValidation
        },
        {
          path: 'payment',
          name: 'Payment',
          component: Payment
        }
      ]
    }, {
      path: '/search/:geohash',
      name: 'Search',
      component: Search
    }, {
      path: '/find',
      name: 'Find',
      component: Find
    }, {
      path: '/order',
      name: 'Order',
      component: Order,
      children: [
        {
          path: 'detail',
          name: 'OrderDetail',
          component: OrderDetail
        }
      ]
    }, {
      path: '/profile',
      name: 'Profile',
      component: Profile,
      children: [
        {
          path: 'info',
          name: 'AccountInfo',
          component: AccountInfo,
          children: [
            {
              path: 'username',
              name: 'UserName',
              component: UserName
            },
            {
              path: 'modifyPassword',
              name: 'forget',
              component: Forget
            }
          ]
        }, {
          path: 'address',
          name: 'Address',
          component: Address,
          children: [
            {
              path: 'add',
              name: 'Add',
              component: Add,
              children: [
                {
                  path: 'search',
                  name: 'SearchAddress',
                  component: SearchAddress
                }
              ]
            }
          ]
        }
      ]
    }
  ],
  scrollBehavior (to, from, savedPosition) {
    return { x: 0, y: 0 }
  }
})
